
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
#include <list>
#include <vector>

#include "Epiplon.h"
#include "../WellSizedVector2.h"
#include "../PositionVector.h"



using namespace std;


Epiplon::Epiplon(){
	htmlTable_1.open("/work/DocIDs/htmlTable_1", ios::binary);
	htmlTable_2.open("/work/DocIDs/htmlTable_2", ios::binary);
	
	linkSuperTable.open("/work/superLinkMap/superMap", ios::binary); //supermapa para links	
	
	MaxSuperMapsFile_1 = 32;
	MaxSuperMapsFile_2 = 33;
	//	/work/superMaps_1 : 32 documentos
	for(uint i=0; i<MaxSuperMapsFile_1; i++){
		string name = fileName("/work/superMaps_1/SuperMapsFile_", i);
		SuperMapsFile_1[i].open(name.c_str(), ios::binary);
	}
	//	/work/superMaps_2 : 33 documentos
	for(uint i=0; i<MaxSuperMapsFile_2; i++){
		string name = fileName("/work/superMaps_2/SuperMapsFile_", i);
		SuperMapsFile_2[i].open(name.c_str(), ios::binary);
	}
	
	createSuperTablesLimits();
}

Epiplon::~Epiplon(){}

void Epiplon::createSuperTablesLimits(void){
	string converter;
	//Limites de Table 1
	converter = "080686c5ca1c730b";
	stringstream(converter) >> hex >> vectosSuperLimits_1[0]; 
	converter = "1001b5095a445582";
	stringstream(converter) >> hex >> vectosSuperLimits_1[1];
	converter = "18df6629624144e1";
	stringstream(converter) >> hex >> vectosSuperLimits_1[2];
	converter = "20e08dae325efad5";
	stringstream(converter) >> hex >> vectosSuperLimits_1[3];
	converter = "28f6e9846af77053";
	stringstream(converter) >> hex >> vectosSuperLimits_1[4];
	converter = "3151c70b5883723e";
	stringstream(converter) >> hex >> vectosSuperLimits_1[5];
	converter = "39de97f3d870d357";
	stringstream(converter) >> hex >> vectosSuperLimits_1[6];
	converter = "3f83fb52cdb625c0";
	stringstream(converter) >> hex >> vectosSuperLimits_1[7];
	converter = "48e2b7a31f9277d8";
	stringstream(converter) >> hex >> vectosSuperLimits_1[8];
	converter = "514948036b90d05e";
	stringstream(converter) >> hex >> vectosSuperLimits_1[9];
	converter = "5a635d448c134fd8";
	stringstream(converter) >> hex >> vectosSuperLimits_1[10];
	converter = "62cbb895139d1a72";
	stringstream(converter) >> hex >> vectosSuperLimits_1[11];
	converter = "6b62e3906b7a4815";
	stringstream(converter) >> hex >> vectosSuperLimits_1[12];
	converter = "73dd4de45d183cd6";
	stringstream(converter) >> hex >> vectosSuperLimits_1[13];
	converter = "7ad53003056a3ea6";
	stringstream(converter) >> hex >> vectosSuperLimits_1[14];
	converter = "82f726a37701c67a";
	stringstream(converter) >> hex >> vectosSuperLimits_1[15];
	converter = "899b30805cf3965d";
	stringstream(converter) >> hex >> vectosSuperLimits_1[16];
	converter = "91fac433510065cd";
	stringstream(converter) >> hex >> vectosSuperLimits_1[17];
	converter = "9a1a99f4f8a17e93";
	stringstream(converter) >> hex >> vectosSuperLimits_1[18];
	converter = "a269200c7e3ced43";
	stringstream(converter) >> hex >> vectosSuperLimits_1[19];
	converter = "aa3376b5e07dac6d";
	stringstream(converter) >> hex >> vectosSuperLimits_1[20];
	converter = "b1d06a1202f64b05";
	stringstream(converter) >> hex >> vectosSuperLimits_1[21];
	converter = "b988a2c4ae62c523";
	stringstream(converter) >> hex >> vectosSuperLimits_1[22];
	converter = "c0f803e9942043af";
	stringstream(converter) >> hex >> vectosSuperLimits_1[23];
	converter = "c78a2fc513a17b39";
	stringstream(converter) >> hex >> vectosSuperLimits_1[24];
	converter = "cf43514abc561903";
	stringstream(converter) >> hex >> vectosSuperLimits_1[25];
	converter = "d6edc4fbbf4ceb0e";
	stringstream(converter) >> hex >> vectosSuperLimits_1[26];
	converter = "dfa214cfa13c105c";
	stringstream(converter) >> hex >> vectosSuperLimits_1[27];
	converter = "e82cfad7541ea2bb";
	stringstream(converter) >> hex >> vectosSuperLimits_1[28];
	converter = "f05f48164d840510";
	stringstream(converter) >> hex >> vectosSuperLimits_1[29];
	converter = "f852c0d872f551c5";
	stringstream(converter) >> hex >> vectosSuperLimits_1[30];
	converter = "ffffffd6bf40797b";
	stringstream(converter) >> hex >> vectosSuperLimits_1[31];

	//Limites de Table 2
	converter = "7af5588f25a3f76";
	stringstream(converter) >> hex >> vectosSuperLimits_2[0]; 
	converter = "f84be2d1b504ff4";
	stringstream(converter) >> hex >> vectosSuperLimits_2[1];
	converter = "1810c165e583dc5a";
	stringstream(converter) >> hex >> vectosSuperLimits_2[2];
	converter = "1ffabe319ed7d110";
	stringstream(converter) >> hex >> vectosSuperLimits_2[3];
	converter = "27db2386d7cf8dbf";
	stringstream(converter) >> hex >> vectosSuperLimits_2[4];
	converter = "30068c38efc05de8";
	stringstream(converter) >> hex >> vectosSuperLimits_2[5];
	converter = "37f0d35a5f6abdc2";
	stringstream(converter) >> hex >> vectosSuperLimits_2[6];
	converter = "3e2097ba7b46b95f";
	stringstream(converter) >> hex >> vectosSuperLimits_2[7];
	converter = "466ea83c0eda331a";
	stringstream(converter) >> hex >> vectosSuperLimits_2[8];
	converter = "4eb43f4857f1c781";
	stringstream(converter) >> hex >> vectosSuperLimits_2[9];
	converter = "57c466bdffd42931";
	stringstream(converter) >> hex >> vectosSuperLimits_2[10];
	converter = "5fb558e460e6ea25";
	stringstream(converter) >> hex >> vectosSuperLimits_2[11];
	converter = "684f76dabb7cfb48";
	stringstream(converter) >> hex >> vectosSuperLimits_2[12];
	converter = "70d05aee38faa88a";
	stringstream(converter) >> hex >> vectosSuperLimits_2[13];
	converter = "77efc561738f7c89";
	stringstream(converter) >> hex >> vectosSuperLimits_2[14];
	converter = "7f752cd96efa28fb";
	stringstream(converter) >> hex >> vectosSuperLimits_2[15];
	converter = "8749cb2d40990b46";
	stringstream(converter) >> hex >> vectosSuperLimits_2[16];
	converter = "8de3d205b349326b";
	stringstream(converter) >> hex >> vectosSuperLimits_2[17];
	converter = "95e3e2186f8eb44f";
	stringstream(converter) >> hex >> vectosSuperLimits_2[18];
	converter = "9e011e2376ab0e50";
	stringstream(converter) >> hex >> vectosSuperLimits_2[19];
	converter = "a56709d7e1d6d930";
	stringstream(converter) >> hex >> vectosSuperLimits_2[20];
	converter = "add37100684b7dfa";
	stringstream(converter) >> hex >> vectosSuperLimits_2[21];
	converter = "b45e998d797adc75";
	stringstream(converter) >> hex >> vectosSuperLimits_2[22];
	converter = "bc69e65752b0f223";
	stringstream(converter) >> hex >> vectosSuperLimits_2[23];
	converter = "c32766d8e9824c5d";
	stringstream(converter) >> hex >> vectosSuperLimits_2[24];
	converter = "ca2b108b3acd0019";
	stringstream(converter) >> hex >> vectosSuperLimits_2[25];
	converter = "d1437c86a6851e55";
	stringstream(converter) >> hex >> vectosSuperLimits_2[26];
	converter = "d960edb9fba4e409";
	stringstream(converter) >> hex >> vectosSuperLimits_2[27];
	converter = "e232763bb9f7e5a0";
	stringstream(converter) >> hex >> vectosSuperLimits_2[28];
	converter = "ea2800b4d290c0f5";
	stringstream(converter) >> hex >> vectosSuperLimits_2[29];
	converter = "f24c6058fd594861";
	stringstream(converter) >> hex >> vectosSuperLimits_2[30];
	converter = "fa1d17086254767a";
	stringstream(converter) >> hex >> vectosSuperLimits_2[31];
	converter = "ffffffaf8b2f1c26";
	stringstream(converter) >> hex >> vectosSuperLimits_2[32];	
}


string Epiplon::fileName(string base, uint id){
	string nameFile = base;
	stringstream convert;
	convert << hex << id << dec;
	nameFile = nameFile + convert.str();
	return nameFile;
}



void Epiplon::loadFastHTML(){
	ifstream fastHTMLTable_1_in("/work/DocIDs/FastHTMLTable_1");
	ifstream fastHTMLTable_2_in("/work/DocIDs/FastHTMLTable_2");

	ifstream fastLinkSuperTable_in("/work/DocIDs/fastLinkSuperTable"); //Bagio sugao

	string linha_in;
	uint pos;
	unsigned long id;
	uint value;
	while(!fastHTMLTable_1_in.eof()){
		fastHTMLTable_1_in >> linha_in;
		if(fastHTMLTable_1_in.eof()) continue;
		pos = linha_in.find_first_of(":");
		if(pos == string::npos) continue;
		stringstream(linha_in.substr(0, pos)) >> dec >> id;
		stringstream(linha_in.substr(pos+1,linha_in.size()-pos-1)) >> hex >> value;
//		cout << "Linha: " << linha_in << endl;
//		cout << "ID = " << dec << id << " - " << hex << value << dec << endl;
		fastHTMLTable_1[id] = value;
	}
	while(!fastHTMLTable_2_in.eof()){
		fastHTMLTable_2_in >> linha_in;
		if(fastHTMLTable_2_in.eof()) continue;
		pos = linha_in.find_first_of(":");
		if(pos == string::npos) continue;
		stringstream(linha_in.substr(0, pos)) >> dec >> id;
		stringstream(linha_in.substr(pos+1,linha_in.size()-pos-1)) >> hex >> value;
//		cout << "Linha: " << linha_in << endl;
//		cout << "ID = " << dec << id << " - " << hex << value << dec << endl;
		fastHTMLTable_2[id] = value;
	}
	unsigned long long id2;
	while(!fastLinkSuperTable_in.eof()){
		fastLinkSuperTable_in >> linha_in;
		if(fastLinkSuperTable_in.eof()) continue;
		pos = linha_in.find_first_of(":");
		if(pos == string::npos) continue;
		stringstream(linha_in.substr(0, pos)) >> hex >> id2;
		stringstream(linha_in.substr(pos+1,linha_in.size()-pos-1)) >> hex >> value;
//		cout << "Linha: " << linha_in << endl;
//		cout << "ID = " << hex << id2 << " - " << hex << value << dec << endl;
		fastLinkSuperTable[id2] = value;
	}

}


void Epiplon::loadFastTable(){
	ifstream fastTable_1_in("/work/DocIDs/FastTable_1");
	ifstream fastTable_2_in("/work/DocIDs/FastTable_2");
	string linha_in;
	uint pos;
	unsigned long long id;
	uint value;
	while(!fastTable_1_in.eof()){
		fastTable_1_in >> linha_in;
		if(fastTable_1_in.eof()) continue;
		pos = linha_in.find_first_of(":");
		if(pos == string::npos) continue;
		stringstream(linha_in.substr(0, pos)) >> hex >> id;
		stringstream(linha_in.substr(pos+1,linha_in.size()-pos-1)) >> hex >> value;
//		cout << "Linha: " << linha_in << endl;
//		cout << "ID = " << hex << id << " - " << hex << value << dec << endl;
		fastTable_1[id] = value;
	}
	while(!fastTable_2_in.eof()){
		fastTable_2_in >> linha_in;
		if(fastTable_2_in.eof()) continue;
		pos = linha_in.find_first_of(":");
		if(pos == string::npos) continue;
		stringstream(linha_in.substr(0, pos)) >> hex >> id;
		stringstream(linha_in.substr(pos+1,linha_in.size()-pos-1)) >> hex >> value;
//		cout << "Linha: " << linha_in << endl;
//		cout << "ID = " << hex << id << " - " << hex << value << dec << endl;
		fastTable_2[id] = value;
	}
}



//unsigned long htmlDocID(void){
//	unsigned long mask_ID = infoTable->contNumPages;
//	unsigned long mask;
//	// infoTable->file_ID - usar para id ?
//	// Colocar ID e colocar num var para numero de paginas lidas por ID
//	mask = 0x007FFFFF;
//	mask_ID = (0x1FF & infoTable->file_ID)*0x00800000 + (mask & mask_ID);
////		cout << "mask_ID: " << hex << mask_ID << " " << "mask: " << mask << " - " << infoTable->file_ID <<  dec << endl;
//	return mask_ID;
//}

void Epiplon::getLinkFromID(uint docId, string & link){
	uint id;
	uint posLine;
	uint pos1, pos2;
	string line;
	link = "";
	id = docId / 0x00800000; //recupera ID do documento que continha a pagina.
//	cout << "ID = " << id << endl;
//	cout << "ID resulta em [1]: " << fastHTMLTable_1[docId] << endl;
//	cout << "ID resulta em [2]: " << fastHTMLTable_2[docId] << endl;
	
	if(id%2){ //se impar, esta em fastHTMLTable_1
		posLine = fastHTMLTable_1[docId];
		//posiciona
		htmlTable_1.clear();
		htmlTable_1.seekg(posLine);
		htmlTable_1 >> line;
		pos1 = line.find("**");
		if(pos1 == string::npos) return;
		pos2 = line.find("**", pos1+1);
		if(pos2 == string::npos) return;
		line = line.substr(pos1+2, pos2-pos1-2);
	}else{    // se par, esta em fastHTMLTable_2
		posLine = fastHTMLTable_2[docId];
		//posiciona
		htmlTable_2.clear();
		htmlTable_2.seekg(posLine);
		htmlTable_2 >> line;
		pos1 = line.find("**");
		if(pos1 == string::npos) return;
		pos2 = line.find("**", pos1+1);
		if(pos2 == string::npos) return;
		line = line.substr(pos1+2, pos2-pos1-2);	
	}
	link = line;
	return;
}

void Epiplon::getSuperDocFromWord(unsigned long long wordID, uint & id1, uint & id2){

	//determina arquivo da Table 1
	id1 = 0;
	while(wordID > vectosSuperLimits_1[id1]){
		id1++;
		if(id1 >= MaxSuperMapsFile_1){
			id1 = MaxSuperMapsFile_1 - 1;
			break;
		}
	}
	//determina arquivo da Table 1
	id2 = 0;
	while(wordID > vectosSuperLimits_2[id2]){
		id2++;
		if(id2 >= MaxSuperMapsFile_2){
			id2 = MaxSuperMapsFile_2-1;
			break;
		}
	}
}

void Epiplon::decodeKeywordString(vector<pair<uint, uint> > &myVec, const string & infoLine) {
	uint posA, posB;
	uint elementoCont = 0;
	posA = 0;
	uint myVecSize = 0;
	pair<uint, uint> auxPair;
	
//	cout << "line: " << infoLine << endl;
	

	myVec.clear();
	while (posA < infoLine.size()) {

		posB = infoLine.find_first_of(":",posA);
		if(posB == string::npos) break;
		
		switch (elementoCont) {
		case 0:
			elementoCont++;
			break;
		case 1:
			stringstream(infoLine.substr(posA, posB-posA)) >> dec >> myVecSize;
			myVec.reserve(myVecSize);
			elementoCont++;
			break;
		case 2:
			stringstream(infoLine.substr(posA, posB-posA)) >> hex >> auxPair.first;
			elementoCont++;
			break;
		case 3:
			stringstream(infoLine.substr(posA, posB-posA)) >> hex >> auxPair.second;
			myVec.push_back(auxPair);
			elementoCont--;
			break;
		}
		posA = posB + 1;
	}
	cout << dec;
//	vector<pair<uint, uint> >::iterator it;
//	for (it = myVec.begin(); it != myVec.end(); it++) {
//		cout << ": " << it->first << " " << it->second << endl;
//	}
	
}


void Epiplon::getKeywordVectorFromID(unsigned long long wordID, vector<pair<uint, uint> > & auxKeywordVector) {

	//Ve se ficou bom Baggio... =D

	cout << "aki" << endl;
	auxKeywordVector.clear();

//	ifstream superMap("/work/superLinkMap/superMap"); --> Agora em linkSuperTable	
	
//	ofstream flush("flush.txt"); // gambi ?  tira isso depois pq fica lento

//	stringstream ss;
//	ss << wordID;

	string infoLine = "";

//	if (superMap == NULL) {
//		cout << "superMap nao encontrado" << endl;
//		exit(1);
//	}


	uint filePos = fastLinkSuperTable[wordID]; //pega pos para seek do map em memoria

	if (filePos == 0) { //otimizei aki -- escovando bit :P
		string firstWord_str = "85164fe6915";  //hash na pos zero da minha tabela
		unsigned long long firstWord_llong;
		stringstream(firstWord_str) >> hex >> firstWord_llong;
		if(wordID != firstWord_llong) return; //se nao eh a primeira palavra na pos zero retorna pq nao tem 
	}
//	flush << "wordID: " << ss.str() << endl;
//	while (!superMap.eof()) {
//		superMap >> infoLine;

		linkSuperTable.seekg(filePos);  // seek para pos correta
		linkSuperTable >> infoLine;     // le a linha correta do arquivo com o hash pedido 

//		string aux = infoLine.substr(0, infoLine.find(":"));
//		flush << aux << " " << ss.str() << endl; 
//		if (aux == ss.str()) {

//			flush << infoLine << endl;
			decodeKeywordString(auxKeywordVector, infoLine);
//			vector<pair<uint, uint> >::iterator it;
//			for (it = auxKeywordVector.begin(); it != auxKeywordVector.end(); it++) {  //Tira isso depois baggiolino
//				flush << hex << "1: " << it->first << " 2: " << it->second << dec << endl;
//			}
//			break;
//		}
//	}

}


void Epiplon::getWellSizedVectorFromID(unsigned long long wordID, vector<WellSizedVector2> & list_WSV){


//	cout << "ID da palavra: " << wordID << endl;
	
	list_WSV.clear();
	uint id1, id2;

	getSuperDocFromWord(wordID, id1, id2);

//	cout << "ID1: " << id1 << endl;
//	cout << "ID2: " << id2 << endl;
	
//	cout << "Palavra no Arquivo Table 1: " << fileName("/work/superMaps_1/SuperMapsFile_", id1) << endl;
//	cout << "Palavra no Arquivo Table 2: " << fileName("/work/superMaps_2/SuperMapsFile_", id2) << endl;
	
	string line;
	uint pos;
	unsigned long long IDValue_1, IDValue_2;
	
	//pega primeira palavra de cada supermapa candidato
	SuperMapsFile_1[id1].clear();
	SuperMapsFile_1[id1].seekg(0, ios::beg);
	SuperMapsFile_1[id1] >> line;
	pos = line.find(":");
	if(pos == string::npos) return;  //tratar isso
	stringstream(line.substr(0, pos)) >> hex >> IDValue_1;
	SuperMapsFile_2[id2].clear();
	SuperMapsFile_2[id2].seekg(0, ios::beg);
	SuperMapsFile_2[id2] >> line;
	pos = line.find(":");
	if(pos == string::npos) return;  //tratar isso
	stringstream(line.substr(0, pos)) >> hex >> IDValue_2;
	
	
//	cout << "IDValue_1: " <<  hex << IDValue_1 << dec << endl;	
//	cout << "IDValue_2: " <<  hex << IDValue_2 << dec << endl;
	
	//Procura deslocamento nos fastTable_1, fastTable_2
	uint ret_fastTable_1, ret_fastTable_2;
	
	ret_fastTable_1 = fastTable_1[wordID];
	ret_fastTable_2 = fastTable_2[wordID];
	
//	cout << "ret_fastTable_1: " << ret_fastTable_1 << endl;
//	cout << "ret_fastTable_2: " << ret_fastTable_2 << endl;

	
	//Le do Supermapa...
	string SM_line_1, SM_line_2;
	SM_line_1 = "";
	SM_line_2 = "";
	WellSizedVector2 WSV_temp;
	unsigned long long ID_temp; 
	
	//pega do superMapa 1 se existe nele
	if( (IDValue_1 == wordID) || ret_fastTable_1 != 0 ){
		SuperMapsFile_1[id1].clear();
		SuperMapsFile_1[id1].seekg( ret_fastTable_1 );
		SuperMapsFile_1[id1] >> SM_line_1;
		cout << "1.1" << endl;
		DecodeString(WSV_temp,ID_temp,SM_line_1);
		cout << "1.2" << endl;
//		list_ID.push_back(ID_temp);
		list_WSV.push_back(WSV_temp);
		cout << "1.3" << endl;
	}
	//pega do superMapa 2 se existe nele
	if( (IDValue_2 == wordID) || ret_fastTable_2 != 0 ){
		SuperMapsFile_2[id2].clear();
		SuperMapsFile_2[id2].seekg( ret_fastTable_2 );
		SuperMapsFile_2[id2] >> SM_line_2;

		cout << "2.1" << endl;
		DecodeString(WSV_temp,ID_temp,SM_line_2);
		cout << "2.2" << endl;
//		list_ID.push_back(ID_temp);
		list_WSV.push_back(WSV_temp);
		cout << "2.3" << endl;

	}
} 



unsigned long Epiplon::getFrequencyFromID(unsigned long long wordID){

	uint id1, id2;
	getSuperDocFromWord(wordID, id1, id2);


	int tamanhoInLine = 50;  //para ler mais rapido soh um pedaco da linha
	char tempInLine[tamanhoInLine];

	string line;
	uint pos;
	unsigned long long IDValue_1, IDValue_2;
	
	//pega primeira palavra de cada supermapa candidato
	SuperMapsFile_1[id1].clear();
	SuperMapsFile_1[id1].seekg(0, ios::beg);
	
//	SuperMapsFile_1[id1] >> line;
	SuperMapsFile_1[id1].read(tempInLine, tamanhoInLine);
	line.clear();
	line.append(tempInLine, tamanhoInLine);
	
	pos = line.find(":");
	if(pos == string::npos) return 0;  //tratar isso
	stringstream(line.substr(0, pos)) >> hex >> IDValue_1;
	SuperMapsFile_2[id2].clear();
	SuperMapsFile_2[id2].seekg(0, ios::beg);

//	SuperMapsFile_2[id2] >> line;
        SuperMapsFile_2[id2].read(tempInLine, tamanhoInLine);
        line.clear();
	line.append(tempInLine, tamanhoInLine);

	pos = line.find(":");
	if(pos == string::npos) return 0;  //tratar isso
	stringstream(line.substr(0, pos)) >> hex >> IDValue_2;
	
//	cout << "IDValue_1: " <<  hex << IDValue_1 << dec << endl;	
//	cout << "IDValue_2: " <<  hex << IDValue_2 << dec << endl;
	
	//Procura deslocamento nos fastTable_1, fastTable_2
	uint ret_fastTable_1, ret_fastTable_2;
	
	ret_fastTable_1 = fastTable_1[wordID];
	ret_fastTable_2 = fastTable_2[wordID];
	
//	cout << "ret_fastTable_1: " << ret_fastTable_1 << endl;
//	cout << "ret_fastTable_2: " << ret_fastTable_2 << endl;
	
	//Le do Supermapa...
	string SM_line_1, SM_line_2;
	SM_line_1 = "";
	SM_line_2 = "";
	WellSizedVector2 WSV_temp;
	
	unsigned long fw_1, fw_2;
	fw_1 = fw_2 = 0;
	
	//pega do superMapa 1 se existe nele
	if( (IDValue_1 == wordID) || ret_fastTable_1 != 0 ){
		SuperMapsFile_1[id1].clear();
		SuperMapsFile_1[id1].seekg( ret_fastTable_1 );

		// SuperMapsFile_1[id1] >> SM_line_1;
        	SuperMapsFile_1[id1].read(tempInLine, tamanhoInLine);
	        SM_line_1.clear();
		SM_line_1.append(tempInLine, tamanhoInLine);

		//pega ocorrencia
		unsigned long pposA, pposB;
		pposA = pposB = 0;
		pposA = SM_line_1.find(":", pposA+1);
		if(pposA == string::npos) pposA = 0;
		pposA = SM_line_1.find(":", pposA+1);
		if(pposA == string::npos) pposA = 0;
		pposB = SM_line_1.find(":", pposA+1);
		if(pposB == string::npos) pposB = 0;
		if(pposA == pposB) fw_1 = 0;
		else {
			string temp = SM_line_1.substr(pposA+1, pposB-pposA-1); 
			stringstream(temp) >> hex >> fw_1;
		}
	}
	//pega do superMapa 2 se existe nele
	if( (IDValue_2 == wordID) || ret_fastTable_2 != 0 ){
		SuperMapsFile_2[id2].clear();
		SuperMapsFile_2[id2].seekg( ret_fastTable_2 );

		// SuperMapsFile_2[id2] >> SM_line_2;
		SuperMapsFile_2[id2].read(tempInLine, tamanhoInLine);
		SM_line_2.clear();
		SM_line_2.append(tempInLine, tamanhoInLine);

		//pega ocorrencia
		unsigned long pposA, pposB;
		pposA = pposB = 0;
		pposA = SM_line_2.find(":", pposA+1);
		if(pposA == string::npos) pposA = 0;
		pposA = SM_line_2.find(":", pposA+1);
		if(pposA == string::npos) pposA = 0;
		pposB = SM_line_2.find(":", pposA+1);
		if(pposB == string::npos) pposB = 0;
		if(pposA == pposB) fw_2 = 0;
		else{
			string temp = SM_line_2.substr(pposA+1, pposB-pposA-1); 
			stringstream(temp) >> hex >> fw_2;
		}
	}
	
	return (fw_1 + fw_2);
} 



void Epiplon::DecodeString(WellSizedVector2 &tempWSV, unsigned long long &_id, const string & infoLine){
	tempWSV = WellSizedVector2(); //clear acochambrado
	unsigned long long ID = 0;
	unsigned int vectorIndex = 0;
	unsigned int documentsSize = 0;
	unsigned int PositionVectorId = 0;
	unsigned int firstOccurrence = 0;
	unsigned int positionSize = 0;
	unsigned short int unsignedShort = 0;
	
	uint posA, posB;
	uint elementoCont;
				
	elementoCont = 0;
	posA = 0;

	PositionVector * tempPos;
//	WellSizedVector2 tempWSV;
//	string infoLine = _line;
	
	while(posA < infoLine.size()){
		
		posB = infoLine.find_first_of(":",posA);
		if(posB == string::npos) break;
		
		if(elementoCont == 0){
			stringstream(infoLine.substr(posA, posB-posA)) >> hex >> ID;
//			cout << "ID: " << ID << endl;
		}
		else if(elementoCont == 1){
			stringstream(infoLine.substr(posA, posB-posA)) >> hex >> vectorIndex;
//			cout << "\t vectorIndex: " << vectorIndex << endl;
//			tempWSV.vectorIndex = vectorIndex; //retirado na ver. 2
//			tempWSV.initializeVector();
		}
		else if(elementoCont == 2){
			stringstream(infoLine.substr(posA, posB-posA)) >> hex >> documentsSize;
			tempWSV.reserve(documentsSize);
//			cout << "\t documentsSize: " << documentsSize << endl;
		}
		else if(elementoCont == 3){
			stringstream(infoLine.substr(posA, posB-posA)) >> hex >> PositionVectorId;
//			cout << "\t\t PositionVectorId: " << PositionVectorId << endl;
			tempPos = new PositionVector(PositionVectorId, 0);
//			tempPos->id = PositionVectorId;
			tempPos->positions.clear();
		}
		else if(elementoCont == 4){
			stringstream(infoLine.substr(posA, posB-posA)) >> hex >> firstOccurrence;
//			cout << "\t\t firstOccurrence: " << firstOccurrence << endl;
			tempPos->firstOccurrence = firstOccurrence;
		}
		else if(elementoCont == 5){
			stringstream(infoLine.substr(posA, posB-posA)) >> hex >> positionSize;
//			cout << "\t\t positionSize: " << positionSize << endl;
			tempPos->initializeVector(positionSize);
		}
		else if(elementoCont < 6 +  positionSize){  //pena numShorts
			stringstream(infoLine.substr(posA, posB-posA)) >> hex >> unsignedShort;
//			cout << "\t\t\t unsignedShort: " << unsignedShort << endl;
			tempPos->insertPosition(unsignedShort);
		}
		else {  // pega mais info dos vetores retornando para PositionVectorId
			elementoCont = 3;
//			cout << "Pushing" << endl;
			tempWSV.documents.push_back(tempPos);
			continue;
		}
		posA = posB+1;
		elementoCont++;

	}
	//pega o ultimo
	if(tempPos->positions.size() != 0){
		tempWSV.documents.push_back(tempPos);
//		cout << "Pushing" << endl;
	}
	
	if(positionSize == 0){
		tempWSV.documents.push_back(tempPos);
//		cout << "Pushing" << endl;
//		cout << "Deu Zero !!! " << endl;
	}
	
	
//	_wsv = tempWSV;
	_id = ID;
	cout << dec;
		
//	cout << "ID: " << ID << endl;
//	cout << "\tvectorIndex: " << vectorIndex << endl;
//	cout << "\tdocumentsSize: " << documentsSize << endl;
//	cout << "\t\tPositionVectorId: " << PositionVectorId << endl;
//	cout << "\t\tpositionSize: " << positionSize << endl; 
//	cout << "\t\t\tunsignedShort: " << unsignedShort << endl; 
}

void Epiplon::status(){
	
	cout << "Tamanho fastHTMLTable_1: " << fastHTMLTable_1.size() << endl;
	cout << "Tamanho fastHTMLTable_2: " << fastHTMLTable_2.size() << endl;
	
	cout << "Tamanho fastTable_1: " << fastTable_1.size() << endl;
	cout << "Tamanho fastTable_2: " << fastTable_2.size() << endl;
}







